package com.qyl.sever_rpc.client;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib 动态代理
 * */
public class CGlibProxy<T> implements MethodInterceptor {

    private T target;

    public CGlibProxy(T target) {
        this.target = target;
    }

    public T createProxyObject() {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        Object proxyObj = enhancer.create();
        return (T)proxyObj;
    }


    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        return method.invoke(target, args);
    }
}
